//
//  M_JifenVC.swift
//  letuofu
//
//  Created by WH-JS on 2020/1/10.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit
import MBProgressHUD
import MJRefresh

class M_JifenVC: BaseViewControllerWithHeader {
    var pageIndex = 1
    var pageSize = 20
    var totalSize = 2000
    var curflowTypeCode = "108003"
    
    var dataListAry = [[String:Any?]]()
    //头部导航
    var tabHeader = SlidingTabHeaderView(screenWidth:375.0, height: 43.0,titles:["历史记录","收入记录","支出记录"],titleSpace:110)
    //列表控件
    var tableView = QuickTableView<[String:Any?],M_JifenCell>()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
//        refreshAll()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    func getDataList(flowTypeCode :String,pageIndex: Int, pageSize:Int) {
        MBProgressHUD.showOrIncrease()
        let params :[String:Any] = ["identityToken":LocalStorage.getString(forKey: USER_IDENTITYTOKEN) ?? "",
                                    "flowTypeCode":flowTypeCode,
                                    "pageIndex":pageIndex,
                                    "pageSize":pageSize,
                                    "sort":"118001",
                                    "dir":"106002"]
        API.request(url: .M_GetPagingMemCentsAccountFlow, params: params) { (result) in
            MBProgressHUD.hideOrDecrease()
            if result.success {
                if let dl = result.data?.getDictionary("data") {
                    for item in dl.getDictList("items") ?? [] {
                        self.dataListAry.append(item)
                    }
                    self.queryData()
                    self.pageIndex = dl.getInt("pageIndex") ?? 0
                    self.totalSize = dl.getInt("totalSize") ?? 0
                }
            }
        }
    }
}


// MARK:- UI
extension M_JifenVC {
    func setupUI() {
        headView.backgroundColor = .white
        safeView.backgroundColor = .xF2F2F2
        headTitle.text = "积分明细"
        
//        drawRightButton(imageName: "xiaoxi_4")
//        .addTarget(self, action: #selector(_xiaoxiButtonClick(button:)), for: .touchUpInside)
        let newsButton = drawRightButton(imageName: "xiaoxi_4")
        newsButton.addTarget(self, action: #selector(_xiaoxiButtonClick(button:)), for: .touchUpInside)
        UnreadPointForNewsButton(button: newsButton, pointRadius: 3.5,
                                 pointOffset: .init(x: -9.5, y: 10),  color: .xE0551B)

        //画横线
        let line = headView.drawView { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        line.backgroundColor = .xF2F2F2
        
        safeView.addSubview(tabHeader)
        tabHeader.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(43.0)
        }
        tabHeader.callSelBlock { (index) in
            switch index {
            case 1:
                    self.curflowTypeCode = "108001"
                
            case 2:
                  self.curflowTypeCode = "108002"
                
            case 3:
                  self.curflowTypeCode = "108003"

                default:
                    self.curflowTypeCode = "108003"
            }
            self.refreshAll()
        }
        tabHeader.onTitleSelected(button: tabHeader.buttons[0])

        
        //添加列表
        safeView.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(tabHeader.snp.bottom).offset(10)
            make.bottom.equalToSuperview()
        }
        tableView.backgroundColor = .xF7F7F7
                        
        //当点击表格行时
        tableView.onDataSelectedSubject().bindWeak(self) { (data, s) in
        }
        
        //刷新相关事件绑定
        tableView.onSetCellSubject().bindWeak(self) { (cell, s) in
        }
        
        //当表底上拉刷新更多时
        tableView.footerLoadMoreSubject.bindWeak(self) { (s) in
            s.getNextPage()
        }
        
        //当表头下拉刷新时
        tableView.headerRequerySubject.bindWeak(self) { (s) in
            s.refreshAll()
            self.tableView.reloadForHeaderRequery(list: self.dataListAry, isLastPage: false)
        }
    }
    
}

// MARK:- 事件
extension M_JifenVC {
    @objc func _xiaoxiButtonClick(button: Button) {
        let vc = M_NewsListVC()
        pushViewController(vc)
    }
    
    func queryData() {
        tableView.list = dataListAry
        tableView.reloadData()
    }
    
    func getNextPage() {
        if pageIndex*pageSize < totalSize {
            getDataList(flowTypeCode: curflowTypeCode, pageIndex: pageIndex+1, pageSize: pageSize)
            tableView.reloadForFooterLoadMore(list: dataListAry, isLastPage: false)
        } else {
            tableView.reloadForFooterLoadMore(list: dataListAry, isLastPage: true)
        }
    }
    
    func refreshAll() {
        dataListAry.removeAll()
        getDataList(flowTypeCode: curflowTypeCode, pageIndex: 1, pageSize: pageSize)
    }
}
